package blog.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import blog.entity.Category;
import blog.service.CategoryService;

@Controller
@RequestMapping("/category")
public class CategoryController {

	@Resource
	private CategoryService categoryService;

	/**
	 * json 传输子级菜单
	 * 
	 * @param parentCateId 父级菜单id
	 * @return
	 */
	@RequestMapping("/listsub")
	@ResponseBody
	public List<Category> listsub(Integer parentCateId) {
		return categoryService.listSub(parentCateId);
	}

	/**
	 * 分类管理页
	 */
	@RequestMapping("")
	public String index(ModelMap m) {
		// 分类列表
		List<Category> categoryList = categoryService.listCategory();
		m.put("categoryList", categoryList);
		return "Category/category-list";
	}

	/**
	 * 分类添加转跳
	 * 
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Category category) {
		categoryService.add(category);
		return "forward:/category";
	}

	/**
	 * 删除公告
	 */
	@RequestMapping("/delete/{categoryId}")
	public String delete(@PathVariable("categoryId") Integer categoryId) {
		categoryService.deleteCategory(categoryId);
		return "forward:/category";
	}
}
